package com.luochen.cdpt.repository;

import com.luochen.cdpt.eneity.OrgInfo;
import com.luochen.cdpt.eneity.QOrgInfo;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.DateTimePath;
import com.querydsl.core.types.dsl.StringPath;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.binding.MultiValueBinding;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@RepositoryRestResource(path = "/org")
public interface IOrgInfoRepository extends PagingAndSortingRepository<OrgInfo,Long>,
        QuerydslPredicateExecutor<OrgInfo>, QuerydslBinderCustomizer<QOrgInfo> {

    default void customize(QuerydslBindings bindings, QOrgInfo orgInfo) {
        //模糊查询
        bindings.bind(orgInfo.orgname).first(
                (StringPath path, String value) -> path.contains(value));
        //时间处理
        bindings.bind(orgInfo.tcrttm).all((path, value) -> {
            List<Date> dateList = (List) value;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            if (value.size() == 1) {
                Date minDate = dateList.get(0);
                booleanBuilder.and(path.eq(minDate));
            }
            if (value.size() >= 2) {
                Date minDate = dateList.get(0);
                Date maxDate = dateList.get(1);
                booleanBuilder.and(path.between(minDate, maxDate));
            }
            return Optional.ofNullable(booleanBuilder);
        });
    }

}
